package com.ruoyi.standingbook.evaluate.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.standingbook.evaluate.domain.bo.EvaluateImportBaseBo;
import com.ruoyi.standingbook.evaluate.domain.vo.EvaluateImportBaseVo;
import com.ruoyi.standingbook.evaluate.domain.vo.EvaluateImportDetailVo;
import com.ruoyi.standingbook.evaluate.service.IEvaluateImportBaseService;
import com.ruoyi.standingbook.evaluate.service.impl.EvaluateExcelService;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;

/**
 * 市环境测评导入
 *
 * @author cc
 * @date 2023-04-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/standingbook/evaluateImportBase")
public class EvaluateImportBaseController extends BaseController {

    private final IEvaluateImportBaseService iEvaluateImportBaseService;
    private final EvaluateExcelService evaluateExcelService;
    /**
     * 查询市环境测评导入列表
     */
    @SaCheckPermission("evaluate:evaluateImportBase:list")
    @GetMapping("/list")
    public TableDataInfo<EvaluateImportBaseVo> list(EvaluateImportBaseBo bo, PageQuery pageQuery) {
        return iEvaluateImportBaseService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出市环境测评导入列表
     */
    @SaCheckPermission("evaluate:evaluateImportBase:export")
    @Log(title = "市环境测评导入", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EvaluateImportBaseBo bo, HttpServletResponse response) {
        List<EvaluateImportBaseVo> list = iEvaluateImportBaseService.queryList(bo);
        ExcelUtil.exportExcel(list, "市环境测评导入", EvaluateImportBaseVo.class, response);
    }

    /**
     * 获取市环境测评导入详细信息
     *
     * @param batchNo 主键
     */
    @SaCheckPermission("evaluate:evaluateImportBase:query")
    @GetMapping("/{batchNo}")
    public R<EvaluateImportBaseVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String batchNo) {
        return R.ok(iEvaluateImportBaseService.queryById(batchNo));
    }

    /**
     * 新增市环境测评导入
     */
    @SaCheckPermission("evaluate:evaluateImportBase:add")
    @Log(title = "市环境测评导入", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EvaluateImportBaseBo bo) {
        return toAjax(iEvaluateImportBaseService.insertByBo(bo));
    }

    /**
     * 修改市环境测评导入
     */
    @SaCheckPermission("evaluate:evaluateImportBase:edit")
    @Log(title = "市环境测评导入", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EvaluateImportBaseBo bo) {
        return toAjax(iEvaluateImportBaseService.updateByBo(bo));
    }

    /**
     * 删除市环境测评导入
     *
     * @param batchNos 主键串
     */
    @SaCheckPermission("evaluate:evaluateImportBase:remove")
    @Log(title = "市环境测评导入", businessType = BusinessType.DELETE)
    @DeleteMapping("/{batchNos}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] batchNos) {
        return toAjax(iEvaluateImportBaseService.deleteWithValidByIds(Arrays.asList(batchNos), true));
    }
    
    /**
     * 新增导入主
     */
    @SaCheckPermission("evaluate:evaluateImportBase:add")
    @Log(title = "市环境测评导入", businessType = BusinessType.IMPORT)
    @RepeatSubmit()
    @PostMapping("/importData")
    public R<Void> importData(@Validated(AddGroup.class) @RequestBody EvaluateImportBaseBo bo) {
        return toAjax(this.evaluateExcelService.importExcel(bo));
    }
    
    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "市环境测评数据", EvaluateImportDetailVo.class, response);
    }
}
